package xyz.thoughtset.viewer.executor.blocks.executor;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import xyz.thoughtset.viewer.common.exc.exceptions.ExecException;
import xyz.thoughtset.viewer.modules.step.entity.BlockTypeEnum;
import xyz.thoughtset.viewer.modules.step.entity.TransposeBody;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockInfo;

import java.util.*;

@Component
public class TransposeBlockExecutor extends AbstractBlockExecutor<TransposeBody> {


    @Override
    BlockTypeEnum getSupportType() {
        return BlockTypeEnum.TRANSPOSE;
    }


    @Override
    protected Object doQuery(BlockInfo block, TransposeBody body, Map<String, Object> params, ExpressionParser parser, StandardEvaluationContext context) throws ExecException {
        //行列转换
        String exp = body.getTargetRepx();
        exp = exp.startsWith("#")?exp:"#"+exp;
        Object value = parser.parseExpression(exp).getValue(context);
        if (ObjectUtils.isEmpty(value)) return value;
        if (value instanceof List){
            List list = (List) value;
            TreeMap<Integer,Object> table = new TreeMap<>();
            int columnLen = list.size();
            for(int i = 0;i<columnLen;i++){
                List oldItemList = (List) list.get(i);
                for(int j = 0;j<oldItemList.size();j++){
                    Object val = oldItemList.get(j);
                    List colList = (List) table.get(j);
                    if (Objects.isNull(colList)){
                        colList = new ArrayList<>(columnLen+1);
                        table.put(j,colList);
                    }
                    colList.add(val);
                }
            }
            ArrayList arrayList = new ArrayList<>(table.size());
            for (Map.Entry<Integer,Object> entry: table.entrySet()){
                arrayList.add(entry.getValue());
            }
            value = arrayList;
        } else if (value instanceof Map) {
            value = mapTranspose((Map<?, ?>) value);
        }
        return value;
    }

    private List mapTranspose(Map<?,?> dataMap){
        int size = dataMap.size();
        List list = new ArrayList<>(2);
        List list0 = new ArrayList<>(size);
        List list1 = new ArrayList<>(size);
        for (Map.Entry entry: dataMap.entrySet()){
            list0.add(entry.getKey());
            list1.add(entry.getValue());
        }
        list.add(list0);
        list.add(list1);
        return list;
    }
//    @Override
//    public Object executeQuery(BlockInfo block, ExpressionParser parser, Map<String, Object> params, StandardEvaluationContext context) throws ExecException {
//        BlockBodyEle bodyEle = block.getFirstBody();
//        List<EleParam> eleParams = bodyEle.getParams();
//        if (Objects.nonNull(eleParams)){
//            for (EleParam blockParam : eleParams){
//                Object value = null;
//                if(!StringUtils.hasLength(blockParam.getDataExp())){
//                    String exp =blockParam.getDataExp();
//                    exp = blockParam.getDataExp().startsWith("#")?exp:"#"+exp;
//                    value = parser.parseExpression(exp).getValue(context);
//                }
//                //qbid作为值对应名称进行存入
//                context.setVariable(blockParam.getParamId(), value);
//                params.put(blockParam.getParamId(), value);
//            }
//        }
//        return null;
//    }
//
//    @Override
//    Object doQuery(BlockInfo block, TransposeBody body, Map<String, Object> params, ExpressionParser parser, StandardEvaluationContext context) throws ExecException {
//        return null;
//    }
}
//'003','数学',85);
//'002','英语',90);
//'003','政治',82);
//003 002 003
//数学 英语 政治
//85  90   82